package kr.saeildamil.androidforstudy2.ch22;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;

public class DrawThread extends Thread {
	boolean bExit;
	int mWidth;
	int mHeight;
	SurfaceHolder mHolder;
	ArrayList<Ball> arBall;

	public DrawThread(SurfaceHolder mHolder, ArrayList<Ball> arBall) {
		super();
		this.mHolder = mHolder;
		this.arBall = arBall;
	}

	public void SizeChange(int width, int height) {
		this.mWidth = width;
		this.mHeight = height;
	}

	@Override
	public void run() {
		Canvas canvas;
		Ball B;

		while (bExit == false) {
			for (int i = 0; i < arBall.size(); i++) {
				B = arBall.get(i);
				B.Move(mWidth, mHeight);
				if (B.count > 4) {
					arBall.remove(i);
					i--;
				}
				
			} // end of for;
			
			synchronized (mHolder) {
				canvas = mHolder.lockCanvas();
				if(canvas==null)break;
				canvas.drawColor(Color.LTGRAY);
				for(int i = 0; i<arBall.size(); i++){
					arBall.get(i).Draw(canvas);
					if(bExit) break;
				}
				mHolder.unlockCanvasAndPost(canvas);
			} // end of synchronized;
			try{sleep(50);}catch(Exception e){};
		}
	}

}
